<template>
  <div class="home-container" ref="box">
    <van-nav-bar class="page-nav-bar" fixed>
      <template #title>
        <van-button round size="small" type="info" icon="search" to="/search"
          >搜索</van-button
        >
      </template>
    </van-nav-bar>

    <van-tabs
      class="channel-tabs"
      v-model="active"
      animated
      swipeable
      line-width="31px"
      title-active-color="#333"
      title-inactive-color="#777"
    >
      <van-tab :title="item.name" v-for="item in channels" :key="item.id">
        <!-- 文章列表组件 -->
        <article-list :channel="item"></article-list>
        <!-- /文章列表组件 -->
      </van-tab>
      <template #nav-right>
        <div class="placeholder"></div>
        <div class="nav-right" @click="showPopup">
          <i class="toutiao toutiao-gengduo"></i>
        </div>
      </template>
    </van-tabs>

    <!-- 频道编辑弹出层 -->
    <van-popup
      v-model="show"
      position="bottom"
      :style="{ height: '100%' }"
      closeable
      close-icon-position="top-left"
    >
      <channel-edit
        :my-channels="channels"
        :active="active"
        @update-active="onUpdateActive"
      ></channel-edit>
    </van-popup>
    <!-- /频道编辑弹出层 -->
  </div>
</template>

<script>
import { getUserChannels } from '@/api/user.js';
import ArticleList from '@/components/ArticleList';
import ChannelEdit from '@/components/ChannelEdit';
import { getItem } from '@/plugins/storage.js';
import { mapState } from 'vuex';

export default {
  name: 'HomePage',
  components: {
    ArticleList,
    ChannelEdit,
  },
  props: {},
  data() {
    return {
      active: 0,
      channels: [], // 频道列表
      show: false,
    };
  },
  computed: {
    ...mapState(['user']),
  },
  watch: {},
  created() {
    this.loadChannels();
  },
  mounted() {},

  methods: {
    onUpdateActive(i, flag) {
      this.active = i;
      this.show = flag;
    },
    // 弹出层
    showPopup() {
      this.show = true;
    },
    async loadChannels() {
      try {
        let channels = [];
        if (this.user) {
          const { data } = await getUserChannels();
          channels = data.data.channels;
        } else {
          const localChannels = getItem('TOUTIAO_CHANNELs');
          if (localChannels) {
            channels = localChannels;
          } else {
            const { data } = await getUserChannels();
            channels = data.data.channels;
          }
        }
        this.channels = channels;
      } catch (err) {
        this.$toast('获取频道数据失败');
      }
    },
  },
};
</script>

<style lang="less" scoped>
.home-container {
  padding-top: 174px;
  /deep/ .van-pull-refresh__head {
    top: 11px;
  }
  .page-nav-bar {
    /deep/ .van-nav-bar__title {
      max-width: unset;
    }
    button {
      width: 554px;
      font-size: 28px;
      background-color: #5babfb;
      border: none;
      .van-icon {
        font-size: 32px;
      }
    }
  }
  /deep/ .channel-tabs {
    .van-tab {
      min-width: 191px;
      border-left: 1px solid #edeff3;
      border-bottom: 1px solid #edeff3;
      font-size: 30px;
    }
    .van-tabs__wrap {
      position: fixed;
      top: 92px;
      z-index: 1;
      left: 0;
      right: 0;
      height: 82px;
    }
    .van-tabs__line {
      height: 6px;
      background-color: #3296fa;
      border-radius: 10px;
      margin-bottom: 8px;
    }
    .right-cover {
      width: 232px;
      height: 146px;
    }
    .van-tabs__nav--complete {
      padding-left: 0;
      padding-right: 0;
    }
    .placeholder {
      flex-shrink: 0;
      width: 66px;
      height: 82px;
    }
    .nav-right {
      position: fixed;
      right: 0;
      width: 66px;
      height: 82px;
      background-color: rgba(255, 255, 255, 0.902);
      line-height: 82px;
      text-align: center;
      i.toutiao {
        font-size: 33px;
        color: #333;
      }
      &:before {
        position: absolute;
        left: 0;
        top: 6px;
        display: block;
        content: '';
        width: 2px;
        height: 70px;
        box-sizing: border-box;
        background: url(~@/assets/gradient-gray-line.png);
        background-size: contain;
      }
    }
  }
}
</style>
